package cz.acies.utils;

import java.io.*;

public class IOUtils 
{	
	
	public IOUtils() {
	}	

	// ----- Output File methods ----------------------------------------------
	
	public static FileOutputStream openOutFile(File file) throws IOException {
		if (!file.exists()) file.createNewFile(); 
		return new FileOutputStream(file);
	}

	public static FileOutputStream openOutFile(String url) throws IOException {		
		File file = new File(url);
		if (!file.exists()) file.createNewFile(); 
		return new FileOutputStream(file);
	}
	
	//----------------------- Input File methods -------------------------------
	
	public static FileInputStream openInFile(File file) throws IOException {
		if (!file.exists()) file.createNewFile(); 
		return new FileInputStream(file);
	}

	public static FileInputStream openInFile(String url) throws IOException {		
		File file = new File(url);
		if (!file.exists()) file.createNewFile(); 
		return new FileInputStream(file);
	}
	
	// -----  -----------------------------------------------------------------
	
	public static File createDirsOnPath(String path) throws Exception {
		File fileDir = new File(path);
		if (!fileDir.exists()) {
			String[] paths = path.split("/");
			if (paths.length > 0) {
				String pathDir = "./";
				for (int i = 0; i < paths.length; i++) {
					pathDir += ((i > 0) ? "/" : "") + paths[i]; 
					File file = new File(pathDir);
					//System.out.println("IOUtils.createDirsOnPath().path = "+file.getPath());
					if (!file.exists()) {
						//System.out.println("IOUtils.createDirsOnPath( NOT EXIST )");
						file.mkdir();
					}
				} 
			}
			return fileDir;
		} else {
			return fileDir; 
		}
	}

};
